package com.ang.hutool.DateUtil;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ssang
 * @date 2021-10-15 14:36
 */
public class Test {
    public static void main(String[] args) {
//        test();
//        getWeekday();
//        getNoLi();
//        forEachMonth();
        Test t = new Test();
        t.test1();
    }

    public static void test(){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        System.out.println(sdf.format(c.getTime()));

        c.add(Calendar.MONTH,-6);
        System.out.println(sdf.format(c.getTime()));

        long dayDiff = DateUtil.between(c.getTime(),new Date(), DateUnit.DAY);
        System.out.println(dayDiff);


        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.DAY_OF_YEAR,1);
        c2.set(Calendar.HOUR,0);
        c2.set(Calendar.MINUTE,0);
        c2.set(Calendar.SECOND,0);
        System.out.println(sdf.format(c2.getTime()));
    }

    //上午下午
    public void dateIsAm(){
        String timeStr = "08:30";
        Date time = DateUtil.parse(timeStr,"HH:mm");
        System.out.println(DateUtil.isPM(time));
    }

    //获取周几
    public static void getWeekday(){
        Date date = new Date();

        int index = DateUtil.dayOfWeek(date);
        System.out.println(index);
    }

    //获取农历
    public static void getNoLi(){
        Date date = new Date();
//        ChineseDate chineseDate = new ChineseDate(date);
        ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate("2022-02-05"));
        System.out.println(chineseDate.getDay());
    }

    //获取当前年，遍历12个月
    public static void forEachMonth(){
        Date date = DateUtil.date();
        System.out.println(DateUtil.format(date,"yyyy-MM"));

        int year = DateUtil.year(date);
        System.out.println(year);
        int month = DateUtil.month(date);
        System.out.println(month);

        System.out.println("=======================");

        date = DateUtil.beginOfYear(date);

        for(int i=0; i<12; i++){
            System.out.println(DateUtil.format(date,"yyyy-M"));
            date = DateUtil.offset(date, DateField.MONTH,1);
        }
    }

    /**
     * 比较日期
     */
    public void test1(){
        System.out.println(DateUtil.parse("2022-10-10"));
        System.out.println(DateUtil.parse("2022-10-10 2:10:10"));
        System.out.println(DateUtil.beginOfDay(DateUtil.date()));
        System.out.println(DateUtil.compare(DateUtil.parseDate("2022-08-22 10:30:00"), DateUtil.beginOfDay(DateUtil.date())) == 0);
        System.out.println(DateUtil.compare(DateUtil.parseDate("2022-08-22 10:30:00"), DateUtil.beginOfDay(DateUtil.date()),"yyyy-MM-dd") == 0);
        System.out.println(DateUtil.compare(DateUtil.parseDate("2022-08-22 10:30:00"), DateUtil.beginOfDay(DateUtil.date()),"yyyy-MM-dd HH:mm:ss") == 0);
        System.out.println(DateUtil.compare(DateUtil.parseDate("2022-08-23 10:30:00"), DateUtil.beginOfDay(DateUtil.date())) > 0);
    }
}
